<?php

namespace backend\models;

use backend\models\SysItem;
use Yii;

/**
 * This is the model class for table "vr_pos".
 *
 * @property integer $id
 * @property integer $vr_space_id
 * @property integer $vr_id
 * @property string $item_id_list
 */
class VrPos extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'vr_pos';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['vr_space_id', 'vr_id'], 'integer'],
            [['item_id_list'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'vr_space_id' => 'Vr Space ID',
            'vr_id' => 'Vr ID',
            'item_id_list' => 'Item Id List',
        ];
    }

    /**
     * vr的空间
     * @param $vrId
     * @return mixed
     */
    public static function getPosForVr($vrId){
       return VrSapce::find()
           ->select('`vr_pos`.*, `vr_sapce`.name, `vr_sapce`.id as space_id ')
           ->leftJoin("(SELECT * from vr_pos where vr_id= {$vrId}) vr_pos",'`vr_pos`.vr_space_id = `vr_sapce`.id')
           ->where("`vr_pos`.`vr_id`={$vrId} OR `vr_pos`.`vr_id` is null")
           ->andWhere("`vr_sapce`.`useing`=1")
           ->asArray()
           ->all();
    }

    public static function getItemInfo($itemId){
        $item = SysItem::findOne((int)$itemId);
        return $item !== null ? $item : null;
    }

    public static function getAllItemByVr($vrId){
        $list = VrPos::find()->where(['vr_id'=>$vrId])->all();
        $arr = [];
        if($list != null){
            foreach ($list as $pos){
                !empty($pos->item_id_list) && array_push($arr, $pos->item_id_list);
            }
        }
        return implode(",", $arr);
    }
    /**
     * 删除单品
     * @param $itemId
     * @param $posId
     */
    public static function deleteItem($itemId, $posId){
        $pos = VrPos::findOne($posId);
        if($pos){
            $newItemList = $pos->item_id_list;
            if(!empty($newItemList)){
                $arr = explode(",",$newItemList);
                if(in_array($itemId, $arr)){
                   $arr = array_diff($arr, [$itemId]);
                }
                $pos->item_id_list = implode(",", $arr);
                return $pos->save();
            }
        }
    }

    /**
     * 添加单品
     * @param $itemId
     * @param $vrId
     * @param $spaceId
     * @return bool
     */
    public static function addItem($itemId, $vrId, $spaceId){
        $model = VrPos::find()->where(['vr_id'=>$vrId, 'vr_space_id'=>$spaceId])->one();
        if($model){
            $arr = explode(',',$model->item_id_list);
            array_push($arr, $itemId);
            $arr = array_unique($arr);
            $model->item_id_list = implode(',', $arr);
        }else{
            $model = new VrPos();
            $model->item_id_list = $itemId;
            $model->vr_id = $vrId;
            $model->vr_space_id = $spaceId;
        }
        return $model->save();
    }
}
